const SpiderBaseClass = require('./SpiderBaseClass');
const querystring = require('querystring');

const listSize = 20;//列表页面有多少个主播信息
const app_info = JSON.stringify({
  'platform': 4,
  'terminal_type': 2,
  'egame_id': 'egame_official',
  'version_code': '9.9.9.9',
  'version_name': '9.9.9.9',
});

class SpiderEGame extends SpiderBaseClass {

  /**
   * 获取第一页列表
   */
  static firstListPage() {
    return 1;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {
    const res = await SpiderEGame.get(SpiderEGame.listPageUrl(page),
      {dataType: 'json', headers: SpiderEGame.headers('https://egame.qq.com')});
    const list = res.data.data.key.retBody.data.live_data.live_list.map(
      room => {
        return {
          anchor: {
            room: room.anchor_id,
            name: room.anchor_name,
            gender: 0,
            avatar: room.anchor_face_url,
          },
          type: room.appname,
          data: {
            num_watch: SpiderEGame.toNumber(room.online),
            num_live: 0,
            num_follower: SpiderEGame.toNumber(room.fans_count),
            num_gift: 0,
          },
        };
      });
    return {
      nextPage: page + 1,
      hasNextPage: list.length >= listSize,
      list,
      page,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
  }

  /**
   * 生成列表页面的网址
   * @param page
   * @return string
   */
  static listPageUrl(page) {
    const param = {
      'key': {
        'module': 'pgg_live_read_ifc_mt_svr',
        'method': 'get_pc_live_list',
        'param': {
          'appid': 'hot',
          'page_num': page,
          'page_size': listSize,
          'tag_id': 0,
          'tag_id_str': '',
        },
      },
    };
    const query = querystring.stringify(
      {param: JSON.stringify(param), app_info});
    return 'https://share.egame.qq.com/cgi-bin/pgg_live_async_fcgi?' + query;
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
  }

}

module.exports = SpiderEGame;